package org.neowei.image.filtering;

import java.awt.image.BufferedImage;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.neowei.image.filter.FilterOption;
import org.neowei.image.filter.VerticalFilter;

public class VerticalFiltering {
	private final int srcheight;
	private final int neowidth;
	private final int neoheight;
	private final BufferedImage outimage;
	private final double[] normalContrib;
	private final int numberhalfdots;
	private final BufferedImage srcimage;

	public VerticalFiltering(FilterOption contributor, int srcheight, int neowidth, int neoheight, BufferedImage srcimage) {
		this.normalContrib = contributor.getNormalContrib();
		this.numberhalfdots = contributor.getNumberhalfdots();
		this.srcheight = srcheight;
		this.neoheight = neoheight;
		this.neowidth = neowidth;
		this.srcimage = srcimage;
		outimage = new BufferedImage(this.neowidth, this.neoheight, BufferedImage.TYPE_INT_RGB);
	}

	public BufferedImage getBufferedImage() {
		ExecutorService service = Executors.newCachedThreadPool();
		for (int y = 0; y <= this.neoheight; y++) {
			int start = 0;
			int Y = (int) ((double) y * srcheight / neoheight + 0.5);
			int startY = Y - numberhalfdots;
			if (startY < 0) {
				startY = 0;
				start = numberhalfdots - Y;
			}
			int stopY = Y + numberhalfdots;
			if (stopY > srcheight - 1) {
				stopY = srcheight - 1;
			}
			service.submit(new VerticalFilter(outimage, srcimage, startY, stopY, start, y, normalContrib, neowidth));
		}
		service.shutdown();
		while (!service.isTerminated())
			;
		return this.outimage;
	}
}
